from PyQt5.QtWidgets import *
from PyQt5 import QtGui, QtCore
from PyQt5.QtGui import QIcon, QPixmap
from PyQt5.QtCore import QSize, Qt

class Helmet(QMainWindow):
    """
    头盔检测主窗口
    """
    def __init__(self):
        super(Helmet, self).__init__()
        self.initUi()

    def initUi(self):
        """
        初始化界面
        """
        # 设置窗口标题
        self.setWindowTitle("头盔检测系统")
        # 设置窗口的最小尺寸
        self.setMinimumSize(1000, 600)
        # 窗口最大化显示
        self.showMaximized()

        # 界面标题
        fun_name = QLabel("基于YOLOv8的深度学习火焰烟雾检测系统")
        fun_name_font = QtGui.QFont()
        fun_name_font.setFamily("华文楷体")
        fun_name_font.setPointSize(30)
        fun_name.setFont(fun_name_font)
        fun_name.setAlignment(QtCore.Qt.AlignCenter)

        # 视频按钮
        # camera_btn = QPushButton("确定")
        # camera_btn.setStyleSheet("border-image: url(:/)")
        # camera_btn.setIcon(QIcon('ui_imgs/icons/camera.png'))

        # GroupBox1
        left_bottom = QGroupBox()
        left_bottom.setTitle("检测结果与位置信息")

        # GroupBox2
        right_bottom = QGroupBox()
        right_bottom.setTitle("组1")

        centerWidget = QWidget(self)

        # layout = QVBoxLayout()
        # 全局布局 - 栅格布局
        layout = QGridLayout()

        # 上部分
        frame_top = QFrame()
        layout_frame_top = QGridLayout()
        layout_frame_top.addWidget(fun_name, 0, 0)
        frame_top.setLayout(layout_frame_top)

        # 初始化下部分左边
        self.initUi_bottom_left()

        # 下部分右边
        frame_bottom_right = QFrame()

        # 下右边使用的布局
        layout_bottom_right = QGridLayout()

        font = QtGui.QFont()
        font.setFamily("华文楷体")
        font.setPointSize(16)

        """ 文件导入 """
        # 创建文件导入QGroupBox
        file_input = QGroupBox("文件导入")
        file_input.setFont(font)

        # 设置QGroupBox的布局
        file_input_layout = QGridLayout()

        # 在QGroupBox中放入控件
        pic_btn = QPushButton()
        pic_btn.setFixedSize(30, 30)
        pic_btn.setStyleSheet("QPushButton { border-image: url(ui/ui_imgs/icons/img.png); }")
        img_file_select = QLineEdit()
        img_file_select.setStyleSheet("QLineEdit { background-color: white; color: black; font-size: 16px; height: 31px; border: 1px solid darkgray; font-family: 华文楷体 }")
        img_file_select.setPlaceholderText("请选择图片文件")
        file_btn = QPushButton()
        file_btn.setFixedSize(30, 30)
        file_btn.setStyleSheet("QPushButton { border-image: url(ui/ui_imgs/icons/folder.png); }")
        file_input_layout.addWidget(pic_btn, 0, 0)
        file_input_layout.addWidget(img_file_select, 0, 1)
        file_input_layout.addWidget(file_btn, 0, 2)

        video_btn = QPushButton()
        video_btn.setFixedSize(30, 30)
        video_btn.setStyleSheet("QPushButton { border-image: url(ui/ui_imgs/icons/video.png); }")
        video_file_select = QLineEdit()
        video_file_select.setStyleSheet("QLineEdit { background-color: white; color: black; font-size: 16px; height: 31px; border: 1px solid darkgray; font-family: 华文楷体 }")
        video_file_select.setPlaceholderText("请选择视频文件")
        file_input_layout.addWidget(video_btn, 1, 0)
        file_input_layout.addWidget(video_file_select, 1, 1)

        camera_btn = QPushButton()
        camera_btn.setFixedSize(30, 30)
        camera_btn.setStyleSheet("QPushButton { border-image: url(ui/ui_imgs/icons/camera.png); }")
        camera_state = QLineEdit()
        camera_state.setStyleSheet("QLineEdit { background-color: white; color: black; font-size: 16px; height: 31px; border: 1px solid darkgray; font-family: 华文楷体 }")
        camera_state.setPlaceholderText("摄像头未开启")
        file_input_layout.addWidget(camera_btn, 2, 0)
        file_input_layout.addWidget(camera_state, 2, 1)

        file_input.setLayout(file_input_layout)

        """ 检测结果 """
        # 创建检测结果QGroupBox
        check_result = QGroupBox("检测结果")
        check_result.setFont(font)

        # 设置QGroupBox的布局
        check_result_layout = QGridLayout()

        # 标签字体
        label_font = QtGui.QFont()
        label_font.setFamily("华文楷体")
        label_font.setBold(True)
        label_font.setPointSize(16)
        label_font.setWeight(500)

        lb_time = QLabel("用时：")
        lb_time.setAlignment(Qt.AlignCenter | Qt.AlignRight)
        lb_time.setFont(label_font)
        lb_time_val = QLabel("23s")
        lb_object = QLabel("目标数目：")
        lb_object.setAlignment(Qt.AlignCenter | Qt.AlignRight)
        lb_object.setFont(label_font)
        lb_object_val = QLabel("5")
        check_result_layout.addWidget(lb_time, 0, 0)
        check_result_layout.addWidget(lb_time_val, 0, 1)
        check_result_layout.addWidget(lb_object, 0, 2)
        check_result_layout.addWidget(lb_object_val, 0, 3)

        lb_objectselect = QLabel("目标：")
        lb_objectselect.setAlignment(Qt.AlignCenter | Qt.AlignRight)
        lb_objectselect.setFont(label_font)
        cmb_objectselect = QComboBox()
        check_result_layout.addWidget(lb_objectselect, 1, 0)
        check_result_layout.addWidget(cmb_objectselect, 1, 1, 1, 2)

        lb_type = QLabel("类型：")
        lb_type.setAlignment(Qt.AlignCenter | Qt.AlignRight)
        lb_type.setFont(label_font)
        lb_type_val = QLabel()
        lb_trustdegree = QLabel("置信度：")
        lb_trustdegree.setAlignment(Qt.AlignCenter | Qt.AlignRight)
        lb_trustdegree.setFont(label_font)
        lb_trustdegree_val = QLabel()
        check_result_layout.addWidget(lb_type, 2, 0)
        check_result_layout.addWidget(lb_type_val, 2, 1)
        check_result_layout.addWidget(lb_trustdegree, 2, 2)
        check_result_layout.addWidget(lb_trustdegree_val, 2, 3)

        lb_obj_loc = QLabel("目标位置：")
        lb_obj_loc.setFont(label_font)
        check_result_layout.addWidget(lb_obj_loc, 3, 0, 1, 4)

        lb_xmin = QLabel("xmin：")
        lb_xmin.setAlignment(Qt.AlignCenter | Qt.AlignRight)
        lb_xmin.setFont(label_font)
        lb_xmin_val = QLabel()
        lb_ymin = QLabel("ymin：")
        lb_ymin.setAlignment(Qt.AlignCenter | Qt.AlignRight)
        lb_ymin.setFont(label_font)
        lb_ymin_val = QLabel()
        check_result_layout.addWidget(lb_xmin, 4, 0)
        check_result_layout.addWidget(lb_xmin_val, 4, 1)
        check_result_layout.addWidget(lb_ymin, 4, 2)
        check_result_layout.addWidget(lb_ymin_val, 4, 3)

        lb_xmax = QLabel("xmax：")
        lb_xmax.setAlignment(Qt.AlignCenter | Qt.AlignRight)
        lb_xmax.setFont(label_font)
        lb_xmax_val = QLabel()
        lb_ymax = QLabel("ymax：")
        lb_ymax.setAlignment(Qt.AlignCenter | Qt.AlignRight)
        lb_ymax.setFont(label_font)
        lb_ymax_val = QLabel()
        check_result_layout.addWidget(lb_xmax, 5, 0)
        check_result_layout.addWidget(lb_xmax_val, 5, 1)
        check_result_layout.addWidget(lb_ymax, 5, 2)
        check_result_layout.addWidget(lb_ymax_val, 5, 3)

        # # 在QGroupBox中放入控件
        # use_time = QLabel("用时：")
        # object_count = QLabel("目标数目：")
        # check_result_layout.addWidget(use_time)
        # check_result_layout.addWidget(object_count)

        check_result.setLayout(check_result_layout)

        """ 操作 """
        # 创建操作QGroupBox
        operation = QGroupBox("操作")
        operation.setFont(font)

        # 设置QGroupBox的布局
        operation_layout = QHBoxLayout()

        # 在QGroupBox中放入控件
        save_btn = QPushButton("保存")
        save_btn.setFixedSize(151, 51)  # 设置按钮大小
        save_btn.setFont(font)  # 设置按钮字体
        save_icon = QIcon("ui\\ui_imgs\\icons\\保存.png")
        save_btn.setIcon(save_icon)
        save_btn.setIconSize(QSize(30, 30))

        exit_btn = QPushButton("退出")
        exit_btn.setFixedSize(151, 51)
        exit_btn.setFont(font)
        exit_icon = QIcon("ui\\ui_imgs\\icons\\退出.png")
        exit_btn.setIcon(exit_icon)
        exit_btn.setIconSize(QSize(30, 30))
        
        operation_layout.addWidget(save_btn)
        operation_layout.addWidget(exit_btn)

        operation.setLayout(operation_layout)

        # 下右侧加入文件导入、检测结果、操作
        layout_bottom_right.addWidget(file_input, 0, 0, 4, 1)
        layout_bottom_right.addWidget(check_result, 4, 0, 7, 1)
        layout_bottom_right.addWidget(operation, 11, 0, 2, 1)

        frame_bottom_right.setLayout(layout_bottom_right)

        # 设置边框格式
        # frame_bottom_right.setStyleSheet("QFrame { border: 1px solid blue} ")

        layout.addWidget(frame_top, 0, 0, 1, 6)
        # layout.addWidget(camera_btn)

        layout.addWidget(self.frame_bottom_left, 1, 0, 9, 4)
        layout.addWidget(frame_bottom_right, 1, 4, 9, 2)

        centerWidget.setLayout(layout)
        self.setCentralWidget(centerWidget)

    def initUi_bottom_left(self):
        """
        创建界面下部的左边
        """
        font = QtGui.QFont()
        font.setFamily("华文楷体")
        font.setPointSize(16)

        # 下部分左边
        self.frame_bottom_left = QFrame()

        # 下部分左边采用网格布局
        bottom_left_layout = QGridLayout()

        # 显示区
        frame_show = QFrame()
        frame_layout = QHBoxLayout()

        lbl_show = QLabel(frame_show)
        # lbl_show.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)
        # frame_show.frameGeometry()
        # lbl_show.setGeometry(QtCore.QRect(0, 0, 770, 480))
        # lbl_show.setGeometry(frame_show.frameGeometry())
        frame_show.setStyleSheet(" QFrame { border: 1px solid red }")
        # lbl_show.setStyleSheet("QLabel { border-image: url(ui/ui_imgs/icons/video.png); }")
        # lbl_show.setStyleSheet("border-image: url(ui/ui_imgs/icons/toukui.png);")
        # lbl_show.setScaledContents(True)
        test = frame_show.size()
        print(frame_show.width(), frame_show.height())

        border_image = QPixmap('ui/ui_imgs/icons/toukui.png')  # 替换为你的图片路径
        lbl_show.setPixmap(border_image)
        lbl_show.setScaledContents(True)
        lbl_show.setMinimumHeight(300)
        # 设置label的尺寸策略
        # lbl_show.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)

        # lbl_show.setStyleSheet(" QLabel { border: 2px solid red }")
        frame_layout.addWidget(lbl_show)

        # 表格展示区
        result_groupbox = QGroupBox("检测结果与位置信息")
        result_groupbox.setFont(font)
        result_groupbox.geometry()

        result_groupbox_layout = QHBoxLayout()

        tableWidget = QTableWidget(result_groupbox)
        tableWidget.setGeometry(QtCore.QRect(10, 30, 900, 90))
        tableWidget.setRowCount(0)
        tableWidget.setColumnCount(5)
        tableWidget.setHorizontalHeaderLabels(['序号', '文件路径', '类别', '置信度', '坐标位置'])
        # item = QTableWidgetItem()
        # tableWidget.setHorizontalHeaderItem(0, item)
        # item = QTableWidgetItem()
        # tableWidget.setHorizontalHeaderItem(1, item)
        # item = QTableWidgetItem()
        # tableWidget.setHorizontalHeaderItem(2, item)
        # item = QTableWidgetItem()
        # tableWidget.setHorizontalHeaderItem(3, item)
        # item = QTableWidgetItem()
        # tableWidget.setHorizontalHeaderItem(4, item)

        # tableWidget.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)

        # result_groupbox_layout.addWidget(tableWidget)
        
        result_groupbox.setLayout(result_groupbox_layout)

        bottom_left_layout.addWidget(frame_show, 0, 0, 4, 1)
        bottom_left_layout.addWidget(result_groupbox, 4, 0, 1, 1)

        self.frame_bottom_left.setLayout(bottom_left_layout)

        # 设置边框格式
        # self.frame_bottom_left.setStyleSheet("QFrame { border: 1px solid red} ")